<template>
  <!-- 添加或修改报工列对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="24" style="margin-top: 30px;">
              <el-form-item label="工序名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入工序名称" />
              </el-form-item>
            </el-col>
            <el-col :span="24" style="margin-top: 30px;">
              <el-form-item label="生产产品名称" prop="gongxuName">
                <el-input v-model="form.gongxuName" placeholder="请输入生产产品名称" />
              </el-form-item>
            </el-col>
            <el-col :span="24" style="margin-top: 30px;">
              <el-form-item label="生产产品型号" prop="gongxuModel">
                <el-input v-model="form.gongxuModel" placeholder="请输入生产产品型号" />
              </el-form-item>
            </el-col>
            <el-col :span="12" style="margin-top: 30px;">
              <el-form-item label="单价" prop="money">
                <el-input-number min="0" v-model="form.money" placeholder="请输入单价" />
              </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
              <el-form-item label="适用车间" prop="deptName">
                <el-select v-model="form.deptName" placeholder="请选择适用车间" style="width: 100%" @change="handshopChange">
                  <el-option v-for="(item, index) in workshopOptions" :key="index" :label="item.label" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="标准工时" prop="workHour">
                <el-input-number v-model="form.workHour" placeholder="请输入标准工时" />
              </el-form-item>
            </el-col> -->
            <el-col :span="24" style="margin-top: 30px;">
              <el-form-item label="图纸号" prop="shuoming">
                <el-input v-model="form.shuoming" placeholder="请输入图纸号" />
              </el-form-item>
            </el-col>
            <el-col :span="24" style="margin-top: 30px;">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addAction, getById, updateAction, getAction } from '@/api/manage';
import moment from 'moment';

export default {
  name: "ModifyMoneyForm",
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      url: {
        getById: '/goodsSaledoGongxuUser/goodsSaledoGongxuUser/',
        edit: '/goodsSaledoGongxuUser/goodsSaledoGongxuUser/edit',
        userTreeSelect: '/system/dept/userTreeSelect'
      },
      workshopOptions: [

      ],
    };
  },
  created() {
    this.getUser();
  },
  methods: {

    handshopChange(e) {
      // 查找匹配的选项，不仅匹配value，也匹配id
      const selected = this.workshopOptions.find(item => item.value === e || item.id === e);
      if (selected) {
        // 设置表单的部门ID和名称
        this.form.deptId = selected.id;
        this.form.deptName = selected.label;
      }
    },
    getUser() {
      let that = this;
      getAction(that.url.userTreeSelect, { parentId: '57d779853f0f4d14b03d51db30099456' }).then(res => {
        that.workshopOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        xuhao: null,
        code: null,
        name: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        shuoming: null,
        status: "0",
        projectId: null,
        projectCode: null,
        projectName: null,
        saleId: null,
        goodsId: null,
        gongyiId: null,
        gongxuId: null,
        gongyiCode: null,
        goodsCode: null,
        goodsName: null,
        goodsModel: null,
        goodsBrand: null,
        doStartTime: null,
        doEndTime: null,
        planStartTime: null,
        planEndTime: null,
        userId: null,
        userName: null,
        money: null,
        count: null,
        allMoney: null,
        remainCount: null,
        deptId: null,
        deptName: null,
        workHour: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    show(row) {
      /*this.reset();
      this.form.money=row.money;
      this.form.parentId=row.id;
      this.open = true;
      this.title = "添加报工";*/
      this.reset();
      const id = row.id;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单产品工序分配任务";
      });
    },
    baogongCount() {
      let that = this;
      if (that.form.money && that.form.count) {
        let totlePrice = this.functionNumberMul(that.form.count, that.form.money);
        that.$set(that.form, "allMoney", totlePrice);
      }
    },
    functionNumberMul(arg1, arg2) {
      var m = 0;
      var s1 = arg1.toString();
      var s2 = arg2.toString();
      try {
        m += s1.split(".")[1].length;
      } catch (e) {
      }
      try {
        m += s2.split(".")[1].length;
      } catch (e) {
      }
      return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报工";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {

          }
        }
      });
    },
  }
};
</script>
